import { useQuery } from '@tanstack/react-query'
import { getCategories, getCategory } from './services'
import { useCategoriesStore } from '../_libs/use-category-store'

const useCategories = () => {
  return useQuery({
    queryKey: ['categories'],
    queryFn: getCategories
  })
}

const useCategory = () => {
  const { selectedCategoryId } = useCategoriesStore()

  return useQuery({
    queryKey: ['categories', { selectedCategoryId }],
    queryFn: () => getCategory(selectedCategoryId!),
    enabled: !!selectedCategoryId
  })
}

export { useCategories, useCategory }
